{layout name="common/base" }
<div class="layui-form layuimini-form">

    <input type="text" hidden value="<?php echo $product_id;?>" name="product_id"><!-- 商品id -->
    <input type="text" hidden value="<?php echo $data['id'];?>" name="id"><!-- 题目id -->

    <div class="layui-form-item">
        <label class="layui-form-label required">题目</label>
        <div class="layui-input-block">
            <input type="text" name="name" lay-verify="required" lay-reqtext="题目不能为空" placeholder="请输入题目" value="<?php echo $data['name'];?>" class="layui-input">
        </div>
    </div>
    <div id="optiondiv">
        <?php foreach ($data['options'] as $k => $vo):
            $ops = array("A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z");
        ?>
        <div class="layui-form-item">
            <!-- <label class="layui-form-label"><?php echo $ops[$k];?>选项：</label> -->
            <label class="layui-form-label">选项：</label>
            <div class="layui-input-block">
                <div class="layui-inline">
                    <input type="text" hidden value="<?php echo $vo['id'];?>" name="idtemp[<?php echo $k;?>]"><!-- 题目id -->
                    <div class="layui-input-inline">
                        <input type="text" name="content[<?php echo $k;?>]" placeholder="请输入选项内容" value="<?php echo $vo['content'];?>" class="layui-input">
                    </div>
                    <label class="layui-form-label">分数</label>
                    <div class="layui-input-inline">
                        <input type="number" name="score[<?php echo $k;?>]" placeholder="请输入选项内容" value="<?php echo $vo['score'];?>" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete" onclick="deletediv('{$vo.id}',this)">删除</a>
                </div>
            </div>
        </div>
        <?php endforeach; ?>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button type="button" class="layui-btn" id="data-add-option-btn">
                <i class="layui-icon">&#xe608;</i> 添加一个选项
            </button>
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="saveBtn">确认保存</button>
        </div>
    </div>
</div>
<script src="__PUBLIC__/lib/jquery-3.4.1/jquery-3.4.1.min.js" charset="utf-8"></script>
<script src="__PUBLIC__/lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
<script src="__PUBLIC__/js/commons.js" charset="utf-8"></script>
<script>
    var url = '/admin/question/edit';
    add_edit(url);

    /*添加选项*/
    var options = new Array("A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z");
    var optioni = "<?php echo count($data['options'])-1;?>";//数组从0开始，进入页面已经创建好三个
    layui.use(['form'], function () {
        var $ = layui.jquery;
        $("#data-add-option-btn").on("click", function () {
            optioni++;
            $("#optiondiv").append(
                '<div class="layui-form-item">'+
                //'<label class="layui-form-label nameoption">'+ options[optioni] +'选项：</label>'+
                '<label class="layui-form-label nameoption">选项：</label>'+
                '<div class="layui-input-block">'+
                '<div class="layui-inline">'+
                '<div class="layui-input-inline">'+
                '<input type="text" name="content['+optioni+']" placeholder="请输入选项内容" value="" class="layui-input">'+
                '</div>'+
                '<label class="layui-form-label">分数</label>'+
                '<div class="layui-input-inline">'+
                '<input type="number" name="score['+optioni+']" placeholder="请输入选项内容" value="" class="layui-input">'+
                '</div>'+
                '</div>'+
                '<div class="layui-inline">'+
                '<a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete" onclick="deletediv2(this);">删除</a>'+
                '</div>'+
                '</div>'+
                '</div>'
            );
        });
    });
    function deletediv2(a) {
        optioni--;
        //$('#optiondiv .layui-form-item').last().remove()
        $(a).parent().parent().parent().remove();
    }
    function deletediv(id,a) {
        layer.confirm('真的删除么?', function (index) {
            $.ajax({
                url: '/admin/Option/del/id/' + id,
                type: 'GET',
                success: function(res) {
                    layer.msg(res.msg, {
                        icon: 1,
                        time: 1000 //2秒关闭（如果不配置，默认是3秒）
                    }, function(){
                        optioni--;
                        //$('#optiondiv .layui-form-item').last().remove()
                        $(a).parent().parent().parent().remove();
                        layer.close(index);
                    });

                },
                error: function(res) {
                    //return   err(res)
                }
            });
        });
    }
</script>
